package controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.UsuarioLoginDAO;
import controllerBeans.UsuarioLogin;

/**
 * Servlet implementation class Login
 */

public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String email = request.getParameter("email");
		String senha = request.getParameter("password");
		
		UsuarioLoginDAO uld = new UsuarioLoginDAO();
		UsuarioLogin ul = new UsuarioLogin();
		ul = uld.getUsuarioLogin(email, senha);
		
		if(ul == null){
			response.sendRedirect("erro.html");
		}else{
			int tipo = ul.getTipo();
			String tipoStr = ""+tipo+"";
			HttpSession session = request.getSession(true);
			session.setAttribute("idUsr", ul.getId());
			session.setAttribute("nameUsr", ul.getNome());
			session.setAttribute("typeUsr", tipoStr);
			response.sendRedirect("account.jsp");
			
		}
		
	}

}
